<template>
  <!--左侧导航菜单-->

  <el-menu
    :collapse="isCollapse"
    default-active="2"
    class="el-menu-vertical-demo"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <h3 v-show="!isCollapse">SAUL-VUE管理系统</h3>
    <h3 v-show="isCollapse">SAUL</h3>
    <el-menu-item
      :index="item.path"
      v-for="item in noChildren"
      :key="item.path"
      @click="clickMenu(item)"
    >
      <i :class="'el-icon-' + item.icon"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>

    <el-submenu
      :index="item.path"
      v-for="(item, index) in hasChildren"
      :key="index"
    >
      <template slot="title">
        <i :class="'el-icon-' + item.icon"></i>
        <span>{{ item.label }}</span>
      </template>

      <el-menu-item
        :index="subitem.path"
        v-for="(subitem, subindex) in item.children"
        :key="subindex"
        @click="clickMenu(subitem)"
      >
        <i :class="'el-icon-' + subitem.icon"></i>
        <span>{{ subitem.label }}</span>
      </el-menu-item>
    </el-submenu>
  </el-menu>
</template>

<script>
// <!--数据渲染菜单-->

export default {
  name: "CommonAside",
  computed: {
    noChildren() {
      //对菜单进行判断 是不是二级菜单分别 return不同的菜单进行渲染
      return this.asideMenu.filter(item => !item.children);
    },
    hasChildren() {
      return this.asideMenu.filter(item => item.children);
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    }
  },
  //菜单数据渲染
  data() {
    return {
      asideMenu: [
        {
          path: "/",
          name: "home",
          label: "首页",
          icon: "s-home"
        },

        {
          path: "/video",
          name: "video",
          label: "视频管理",
          icon: "video-play"
        },

        {
          path: "/user",
          name: "user",
          label: "用户管理",
          icon: "user"
        },

        {
          path: "/shezhi",
          label: "设置",
          name: "shezhi",
          icon: "setting",
          children: [
            {
              path: "/page1",
              label: "页面1",
              name: "page1",
              icon: "loading"
            },
            {
              path: "/page2",
              label: "页面2",
              name: "page2",
              icon: "view"
            }
          ]
        }
      ]
    };
  },
  methods: {
    clickMenu(item) {
      this.$router.push({ name: item.name });
      this.$store.commit("selectMenu", item);
    }
  }
};
</script>

<style lang="scss" scoped>
.el-menu {
  height: 100%;
  border: none;
  h3 {
    color: #ffffff;
    text-align: center;
    line-height: 48px;
  }
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
